package com.chinatsp.onlineapp.presenter;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import tsp.cn.http.BaseIDao;
import tsp.cn.http.HttpConfig;
import tsp.cn.http.interfac.INetResult;
import tsp.cn.http.tools.LogUtil;
import android.content.Context;

import com.chinatsp.onlineapp.base.Config;
import com.chinatsp.onlineapp.bean.WeatherService;
import com.google.gson.Gson;

/**
 * 天气服务接口
 */

public class WeatherServiceDao extends BaseIDao {

    private WeatherService mWeatherService;
    private Context mContext;

    public WeatherServiceDao(Context context, INetResult iNetResult) {
        super(context, iNetResult);
        this.mContext = context;
    }

    public WeatherService getmWeatherService() {
        return mWeatherService;
    }

    /**
     * 得到结果，对结果进行处理
     *
     * @param result
     * @param requestCode
     */
    @Override
    public void onRequestSuccess(String result, int requestCode) {
        LogUtil.v("---开始处理---" + result);
        mWeatherService = new Gson().fromJson(result, WeatherService.class);
    }

    /**
     * 获取天气信息
     *
     * @param str 城市名称的中文编码
     */
    public void getWeatherService(String str) {

        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(HttpConfig.appendString());
        strBuilder.append(Config.API_WEATHER_SERVICE);
        try {
            strBuilder.append(URLEncoder.encode(str, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        strBuilder.append("/");
        getRequest(strBuilder.toString(), Config.CODE_WEATHER_SERVICE);
    }
    
    public void getWeatherService(String str,int requestCode) {

        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(HttpConfig.appendString());
        strBuilder.append(Config.API_WEATHER_SERVICE);
        try {
            strBuilder.append(URLEncoder.encode(str, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        strBuilder.append("/");
        getRequest(strBuilder.toString(), requestCode);
    }

}
